//
//  HNSH_RechargeViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/6/26.
//

#import "HNSH_RechargeViewController.h"

#import "HNSH_RechargeTopView.h"
#import "HNSH_RechargeBottomView.h"
#import "HNSH_RechargeHeader.h"
#import "HNSH_RechargeCell.h"

#import "HNSH_RechargeModel.h"

#import "HNSH_RechargeRecordViewController.h"

@interface HNSH_RechargeViewController ()

/* topView */
@property (nonatomic, strong) HNSH_RechargeTopView *topView;
/* bottomView */
@property (nonatomic, strong) HNSH_RechargeBottomView *bottomView;
/* dataAry */
@property (nonatomic, strong) NSArray *dataAry;

/* configModel */
@property (nonatomic, strong) HNSH_RechargeModel *configModel;
@end

static NSString *HNSH_RechargeCellID = @"HNSH_RechargeCellID";
static NSString *HNSH_RechargeHeaderID = @"HNSH_RechargeHeaderID";

@implementation HNSH_RechargeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self showNavTitle:@"账户充值" showLeftItem:YES];
    
    [self configTableView];
    
    [self loadCapitalVoucherConfig];
    
    __weak __typeof(&*self)weakSelf = self;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(kScreenWidth-95, kStatusBarHeight, 80, 44);
    [button setTitle:@"充值记录" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [self.navView addSubview:button];
    [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        HNSH_RechargeRecordViewController *vc = [[HNSH_RechargeRecordViewController alloc]init];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
}

- (void)configTableView {
    [self.tableView registerClass:[HNSH_RechargeCell class] forCellReuseIdentifier:HNSH_RechargeCellID];
    [self.tableView registerClass:[HNSH_RechargeHeader class] forHeaderFooterViewReuseIdentifier:HNSH_RechargeHeaderID];
    self.tableView.rowHeight = 32;
    self.tableView.estimatedRowHeight = 32;
    
    self.tableView.tableFooterView = self.bottomView;
    self.tableView.tableHeaderView = self.topView;
}

#pragma mark networking
- (void)loadCapitalVoucherConfig {
    [HNSH_UserNetServer requestCapitalVoucherConfigSuccess:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            self.configModel = [HNSH_RechargeModel yy_modelWithDictionary:responseObject.resultset];
            [self.tableView reloadData];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

- (void)loadCapitalVoucherApply {
    if (![self.topView.textField.text isNotBlank]) {
        [self.view makeToast:@"请输入充值金额"];
        return;
    }
    if (![self.topView.imgString isNotBlank]) {
        [self.view makeToast:@"请上传图片"];
        return;
    }
    [HNSH_UserNetServer requestCapitalVoucherApplyWithMoney:self.topView.textField.text voucher_url:self.topView.imgString success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [self.view makeToast:@"充值成功"];
            self.topView.textField.text = @"";
            [self.topView.pzButton setImage:[UIImage imageNamed:@"purse_tianjia"] forState:UIControlStateNormal];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark lazy
- (HNSH_RechargeTopView *)topView {
    if(!_topView) {
        _topView = [[HNSH_RechargeTopView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 235)];
    }
    return _topView;
}

- (HNSH_RechargeBottomView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[HNSH_RechargeBottomView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
        WeakSelf(weakSelf);
        _bottomView.bottomBlock = ^{
            [weakSelf loadCapitalVoucherApply];
        };
    }
    return _bottomView;
}

- (NSArray *)dataAry {
    if (!_dataAry) {
        _dataAry = @[
        @[@{@"title":@"账户：",@"des":@""},
          @{@"title":@"银行：",@"des":@""},
          @{@"title":@"卡号：",@"des":@""}],
        
        @[@{@"title":@"账户：",@"des":@""},
          @{@"title":@"支付宝号：",@"des":@""}]];
    }
    return _dataAry;
}

#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataAry.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *ary = self.dataAry[section];
    return ary.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    HNSH_RechargeHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HNSH_RechargeHeaderID];
    if (section == 0) {
        header.leftLabel.text = @"银行转账";
    } else {
        header.leftLabel.text = @"支付宝转账";
    }
    return header;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HNSH_RechargeCell *cell = [tableView dequeueReusableCellWithIdentifier:HNSH_RechargeCellID];
    NSArray *ary = self.dataAry[indexPath.section];
    NSDictionary *dict = ary[indexPath.row];
    cell.leftLabel.text = dict[@"title"];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.rightLabel.text = self.configModel.bank_holder;
        } else if (indexPath.row == 1) {
            cell.rightLabel.text = self.configModel.bank_name;
        } else if (indexPath.row == 2) {
            cell.rightLabel.text = self.configModel.bank_card_no;
        }
    } else {
        if (indexPath.row == 0) {
            cell.rightLabel.text = self.configModel.alipay_name;
        } else if (indexPath.row == 1) {
            cell.rightLabel.text = self.configModel.alipay_account;
        }
    }
    return cell;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
